﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace LX.Web.Utils
{
    public class HttpHelper
    {
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue, HttpContext context = null)
        {
            HttpContext ctx = context ?? HttpContext.Current;
            HttpCookie cookie = ctx.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            ctx.Response.AppendCookie(cookie);

        }



        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue, int expires, HttpContext context = null)
        {
            HttpContext ctx = context ?? HttpContext.Current;
            HttpCookie cookie = ctx.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            ctx.Response.AppendCookie(cookie);

        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName, HttpContext context = null)
        {
            HttpContext ctx = context ?? HttpContext.Current;

            if (ctx != null && ctx.Request.Cookies[strName] != null)
            {
                return ctx.Request.Cookies[strName].Value.ToString();
            }
            return "";
        }


        /// <summary>
        /// 读Session值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>Session值</returns>
        public static string GetSession(string strName)
        {
            if (HttpContext.Current.Session != null && HttpContext.Current.Session[strName] != null)
            //if ( HttpContext.Current.Session[strName] != null)
            {
                string value = HttpContext.Current.Session[strName].ToString();
                return value;
            }

            return "";
        }

        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {


            string result = String.Empty;

            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }

            if (string.IsNullOrEmpty(result) || !System.Text.RegularExpressions.Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"))
            {
                return "0.0.0.0";
            }

            return result;

        }
    }
}
